﻿using System.Globalization;
using System.Text.RegularExpressions;
using ChangeLog.CommunityToolkit.Models;

namespace ChangeLog.CommunityToolkit
{
    public class ChangeLogHelper
    {
        public static Dictionary<string, string> TypeDcit =new Dictionary<string, string>()
        {
            { "Features","新增"},
            { "feat","新增"},
            { "fix","修复 "},
            { "Fixed","修复 "},
            { "Changed","变更 "},
            { "changed","变更 "},
            { "docs","文档 "},
            { "Refactored","优化"},
            { "refactor","优化"},
            { "Deprecated","即将删除"},
            { "deprecated","即将删除"},
            { "Removed","删除"},
            { "removed","删除"},
            { "BREAKING CHANGE","破坏性变更"},
           
        };

        public static string BuildChangLog(string gitlog)
        {
            var changetlog = new Models.ChangeLog();
            //var releaseVersion = new ReleaseVersion();
            var commitList = new List<CommitDetail>();
            var lines = gitlog.Split('\n');
            lines.ToList().ForEach(s =>
            {
                var str = s.Trim();
                if (!string.IsNullOrWhiteSpace(str))
                {
                    if (str.StartsWith("commit"))
                    {//version

                        commitList.Add(new CommitDetail());
                        commitList.Last().Version = Regex.Match(str, @"\d{1,2}\.\d{1,2}\.\d{1,2}").Value;
                    }
                    if (str.StartsWith("Author:"))
                    {//Author
                        commitList.Last().Author = Regex.Match(str, @"(?<txt>(?<=Author:).+(?=\<))").Value;
                    }
                    if (str.StartsWith("Date:"))
                    {//Date
                        CultureInfo enUS = new CultureInfo("en-US");
                        string pattern = "ddd MMM d HH:mm:ss yyyy";
                        DateTime parsedDate = default;
                        var strDate = Regex.Match(str, @"(?<txt>(?<=Date:).+(?=\+))").Value.Trim();
                        if (DateTime.TryParseExact(strDate, pattern, enUS, DateTimeStyles.None, out parsedDate))
                            commitList.Last().Date = parsedDate.ToString("yyyy-MM-dd");
                    }
                    var ok = false;
                    var type = string.Empty;
                    TypeDcit.Keys.ToList().ForEach(r =>
                    {
                        if (str.StartsWith(r))
                        { type = r; ok = true; }

                    });
                    if (ok)
                    {//type,desc
                        var commitInfo = Regex.Match(str, $@"(?<txt>(?<={type}: ).+)").Value.Trim();
                        if (commitList.Last().CommitDictionary.ContainsKey(type))
                            commitList.Last().CommitDictionary[type].Add(commitInfo);
                        else
                            commitList.Last().CommitDictionary.Add(type,new List<string>(){ commitInfo } );
                    }
                }
            });
            //var changetlog.ReleaseVersions = new List<ReleaseVersion>();
            commitList.ForEach(r =>
            {
                if (!string.IsNullOrWhiteSpace(r.Version))
                {//把本次提交，并release版本
                    changetlog.ReleaseVersions.Add(new ReleaseVersion());
                    changetlog.ReleaseVersions.Last().Version = r.Version;
                    changetlog.ReleaseVersions.Last().Date = r.Date;
                    foreach (var commitDictionaryKey in r.CommitDictionary.Keys)
                    {
                        if (changetlog.ReleaseVersions.Last().TypeDescs.ContainsKey(commitDictionaryKey))//聚合type描述
                            changetlog.ReleaseVersions.Last().TypeDescs[commitDictionaryKey].AddRange(r.CommitDictionary[commitDictionaryKey]);
                        else//初始化type描述
                            changetlog.ReleaseVersions.Last().TypeDescs.Add(commitDictionaryKey, r.CommitDictionary[commitDictionaryKey] );
                    }
                }
                else
                {//提交没有release版本
                    if (changetlog.ReleaseVersions.Any())
                    {//只有发布才记入changelog
                        foreach (var commitDictionaryKey in r.CommitDictionary.Keys)
                        {
                            if (changetlog.ReleaseVersions.Last().TypeDescs.ContainsKey(commitDictionaryKey))//聚合type描述
                                changetlog.ReleaseVersions.Last().TypeDescs[commitDictionaryKey].AddRange(r.CommitDictionary[commitDictionaryKey]);
                            else//初始化type描述
                                changetlog.ReleaseVersions.Last().TypeDescs.Add(commitDictionaryKey, r.CommitDictionary[commitDictionaryKey]);
                        }
                    }
                }
            });
            return changetlog.ToString();
        }
    }
}
